package com.shinewind.info.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shinewind.info.mapper.ICommentMapper;
import com.shinewind.info.service.ICommentActionService;
import com.shinewind.info.service.ICommentService;
import com.shinewind.pojo.dto.ParentCommentDto;
import com.shinewind.pojo.dto.ParentDeleteCommentDto;
import com.shinewind.pojo.entity.Comment;
import com.shinewind.pojo.entity.CommentAction;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CommentServiceImpl extends ServiceImpl<ICommentMapper, Comment> implements ICommentService {
    @Autowired
    private ICommentActionService iCommentActionService;

    @Override
    public List<Comment> getCommentListById(Integer aId) {
        List<Comment> comments = this.lambdaQuery().eq(Comment::getAId,aId).orderByDesc(Comment::getPublishTime).list();
        return comments;
    }

    @Override
    public Long getSumLoveByUid(String uId) {
        Long total = 0l;
        //根据uid查询哪些评论是该用户的
        List<Comment> comments = this.lambdaQuery().eq(Comment::getUId,uId).list();
        if(ObjectUtil.isNotNull(comments)){
            for(Comment comment:comments){
                total = total + iCommentActionService.getSumLoveBycId(comment.getCId());
            }
            return total;
        }
        return 0l;
    }

    @Override
    public Boolean saveParentCommentByDto(ParentCommentDto parentCommentDto) {
        Comment comment = Comment.builder().build();
        BeanUtils.copyProperties(parentCommentDto,comment);
        return this.save(comment) == true ? true :false;
    }

    @Override
    public Boolean deleteCommentBycId(ParentDeleteCommentDto parentDeleteCommentDto) {
        Comment comment = Comment.builder().build();
        BeanUtils.copyProperties(parentDeleteCommentDto,comment);
        //删除一级点赞记录
        iCommentActionService.lambdaUpdate().eq(CommentAction::getCId,comment.getCId()).remove();
        //删除二级点赞记录
        //查询二级点赞列表
        List<Comment> comments = this.lambdaQuery().eq(Comment::getAId,comment.getAId())
                .eq(Comment::getParentId,comment.getCId()).list();
        System.out.println("comment:"+comments);
        for(Comment comment1:comments){
            iCommentActionService.lambdaUpdate().eq(CommentAction::getCId,comment1.getCId()).remove();
        }
        //先删除一级评论
        Boolean flag1 = this.lambdaUpdate().eq(Comment::getAId,comment.getAId())
                .eq(Comment::getParentId,'0')
                .eq(Comment::getCId,comment.getCId()).remove();
        //批量删除与其关联的二级评论
        Boolean flag2 = this.lambdaUpdate().eq(Comment::getAId,comment.getAId())
                .eq(Comment::getParentId,comment.getCId()).remove();
        if(flag1 || flag2){
            return true;
        }
        else{
            return false;
        }
    }

    @Override
    public Boolean deleteChildCommentBycId(ParentDeleteCommentDto parentDeleteCommentDto) {
        Comment comment = Comment.builder().build();
        BeanUtils.copyProperties(parentDeleteCommentDto,comment);
        //删除该二级评论
        Boolean flag2 = this.lambdaUpdate().eq(Comment::getAId,comment.getAId())
                .eq(Comment::getCId,comment.getCId()).remove();
        if(flag2){
            //删除相关联的二级点赞记录
            iCommentActionService.lambdaUpdate().eq(CommentAction::getCId,comment.getCId()).remove();
            return true;
        }
        else{
            return false;
        }
    }
}
